In [69]:
# Importing necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn 
from six import StringIO
from sklearn.neighbors import KNeighborsClassifier 
from sklearn.model_selection import train_test_split # Import train_test_split function
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier 
from sklearn.naive_bayes import GaussianNB 
# Load libraries 
from sklearn.svm import SVC
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import VotingClassifier #bagging 
import pickle 
import joblib
from sklearn.metrics import classification_report, confusion_matrix
from IPython.display import Image
from sklearn.tree import export_graphviz 
import pydotplus 
import plotly.graph_objects as go
import plotly.offline as pyo
import plotly.express as px
from plotly.subplots import make_subplots
from plotly import tools
from sklearn.metrics import precision_recall_fscore_support
In [2]:
def reading_csv(filename):
    df=pd.read_csv(filename)
    return df
In [3]:
def extract_features(df):
    X=df.iloc[:,0:7].values
    return X
In [4]:
def extract_target(df):
    Y=df.iloc[:,7].values
    return Y
In [5]:
def split(X,Y):
    X_train,X_test, y_train, y_test=train_test_split(X,Y, test_size=0.3, random_state=1) # 70% training and 30% test
    return X_train,X_test, y_train, y_test

Depression

In [6]:
df_dep=reading_csv("../Datasets/Depression.csv")
X_dep=extract_features(df_dep)
Y_dep=extract_target(df_dep)
X_train_dep,X_test_dep, y_train_dep, y_test_dep=split(X_dep,Y_dep)
In [7]:
dep_cols=df_dep.columns[:-1]
dep_cols
Out[7]:
Index(['Q3(D)', 'Q5(D)', 'Q10(D)', 'Q13(D)', 'Q16(D)', 'Q17(D)', 'Q21(D)'], dtype='object')

Anxiety

In [8]:
df_anx=reading_csv("../Datasets/Anxiety.csv")
X_anx=extract_features(df_anx)
Y_anx=extract_target(df_anx)
X_train_anx,X_test_anx, y_train_anx, y_test_anx=split(X_anx,Y_anx)
In [9]:
anx_cols=df_anx.columns[:-1]
anx_cols
Out[9]:
Index(['Q2(A)', 'Q4(A)', 'Q7(A)', 'Q9(A)', 'Q15(A)', 'Q19(A)', 'Q20(A)'], dtype='object')

Stress

In [10]:
df_str=reading_csv("../Datasets/Stress.csv")
X_str=extract_features(df_str)
Y_str=extract_target(df_str)
X_train_str,X_test_str, y_train_str, y_test_str=split(X_str,Y_str)
In [11]:
str_cols=df_str.columns[:-1]
str_cols
Out[11]:
Index(['Q1(S)', 'Q6(S)', 'Q8(S)', 'Q11(S)', 'Q12(S)', 'Q14(S)', 'Q18(S)'], dtype='object')
In [12]:
def save_model(model,filename):
    with open(filename, 'wb') as file:
        pickle.dump(model, file)
In [13]:
def load_model(filename):
    with open(filename, 'rb') as file:
        model = pickle.load(file)
    return model
In [14]:
def dump_joblib(model,filename):
    joblib.dump(model, filename)

KNN

In [15]:
def KNN(X_train,X_test, y_train, y_test):
    classifier= KNeighborsClassifier(n_neighbors=5) #3 
    classifier.fit(X_train,y_train)
    y_pred=classifier.predict(X_test)
    return classifier,y_pred
In [16]:
# Depression
model,y_pred=KNN(X_train_dep,X_test_dep, y_train_dep, y_test_dep)
# filename="models/dep_model_knn.pkl"
# save_model(model,filename)
knn_acc_dep=accuracy_score(y_test_dep,y_pred)
knn_cls_dep=classification_report(y_test_dep, y_pred)
In [17]:
# Anxiety
model,y_pred=KNN(X_train_anx,X_test_anx, y_train_anx, y_test_anx)
# filename="models/anx_model_knn.pkl"
# save_model(model,filename)
knn_acc_anx=accuracy_score(y_test_anx,y_pred) 
knn_cls_anx=classification_report(y_test_anx, y_pred)
In [18]:
# Stress
model,y_pred=KNN(X_train_str,X_test_str, y_train_str, y_test_str)
# filename="models/str_model_knn.pkl"
# save_model(model,filename)
knn_acc_str=accuracy_score(y_test_str,y_pred)
knn_cls_str=classification_report(y_test_str, y_pred)
In [20]:
model = load_model("../models/dep_model_knn.pkl")
y_pred = model.predict(X_test_dep[[4]])

NaiveBayes

In [21]:
def GNB(X_train,X_test, y_train, y_test):
    gnb = GaussianNB()
    gnb.fit(X_train,y_train)
    y_pred = gnb.predict(X_test)  
    return gnb,y_pred
In [22]:
# Depression
model,y_pred=GNB(X_train_dep,X_test_dep, y_train_dep, y_test_dep)
# filename="models/dep_model_gnb.pkl"
# save_model(model,filename)
gnb_acc_dep=accuracy_score(y_test_dep,y_pred)
gnb_cls_dep=classification_report(y_test_dep, y_pred)
In [23]:
# Anxiety
model,y_pred=GNB(X_train_anx,X_test_anx, y_train_anx, y_test_anx)
# filename="models/anx_model_gnb.pkl"
# save_model(model,filename)
gnb_acc_anx=accuracy_score(y_test_anx,y_pred) 
gnb_cls_anx=classification_report(y_test_anx, y_pred)
In [24]:
# Stress
model,y_pred=GNB(X_train_str,X_test_str, y_train_str, y_test_str)
# filename="models/str_model_gnb.pkl"
# save_model(model,filename)
gnb_acc_str=accuracy_score(y_test_str,y_pred)
gnb_cls_str=classification_report(y_test_str, y_pred)

Decision Tree

In [25]:
def DTree(criteria,X_train,X_test, y_train):
    cls = DecisionTreeClassifier(criterion=criteria, max_depth=10,splitter='best') 
    cls.fit(X_train,y_train) #training of classifier
    y_pred = cls.predict(X_test) 
    return cls,y_pred
In [26]:
# Depression
model,y_pred=DTree('gini',X_train_dep,X_test_dep, y_train_dep)
# filename="models/dep_model_dt.pkl"
# save_model(model,filename)
dt_acc_dep=accuracy_score(y_test_dep,y_pred)
dt_cls_dep=classification_report(y_test_dep, y_pred)
In [27]:
#Decision tree output
dot_data = StringIO()
export_graphviz(model, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True, feature_names = dep_cols,class_names=['0','1','2','3','4'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('depression.png')
Image(graph.create_png())
dot: graph is too large for cairo-renderer bitmaps. Scaling by 0.245365 to fit

dot: graph is too large for cairo-renderer bitmaps. Scaling by 0.245365 to fit

Out[27]:
In [28]:
# Anxiety
model,y_pred=DTree('gini',X_train_anx,X_test_anx, y_train_anx)
# filename="models/anx_model_dt.pkl"
# save_model(model,filename)
dt_acc_anx=accuracy_score(y_test_anx,y_pred) 
dt_cls_anx=classification_report(y_test_anx, y_pred)
In [29]:
#Decision tree output
dot_data = StringIO()
export_graphviz(model, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True, feature_names = anx_cols,class_names=['0','1','2','3','4'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('anxiety.png')
Image(graph.create_png())
dot: graph is too large for cairo-renderer bitmaps. Scaling by 0.246389 to fit

dot: graph is too large for cairo-renderer bitmaps. Scaling by 0.246389 to fit

Out[29]:
In [30]:
# Stress
model,y_pred=DTree('gini',X_train_str,X_test_str, y_train_str)
# filename="models/str_model_dt.pkl"
# save_model(model,filename)
dt_acc_str=accuracy_score(y_test_str,y_pred)
dt_cls_str=classification_report(y_test_str, y_pred)
In [31]:
#Decision tree output
dot_data = StringIO()
export_graphviz(model, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True, feature_names = str_cols,class_names=['0','1','2','3','4'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('stress.png')
Image(graph.create_png())
dot: graph is too large for cairo-renderer bitmaps. Scaling by 0.228009 to fit

dot: graph is too large for cairo-renderer bitmaps. Scaling by 0.228009 to fit

Out[31]:
In [32]:
# Depression
model,y_pred=DTree('entropy',X_train_dep,X_test_dep, y_train_dep)
dt_ent_acc_dep=accuracy_score(y_test_dep,y_pred)
dt_ent_cls_dep=classification_report(y_test_dep, y_pred)
# Anxiety
model,y_pred=DTree('entropy',X_train_anx,X_test_anx, y_train_anx)
dt_ent_acc_anx=accuracy_score(y_test_anx,y_pred) 
dt_ent_cls_anx=classification_report(y_test_anx, y_pred)
# Stress
model,y_pred=DTree('entropy',X_train_str,X_test_str, y_train_str)
dt_ent_acc_str=accuracy_score(y_test_str,y_pred)
dt_ent_cls_str=classification_report(y_test_str, y_pred)

Random Forest

In [33]:
def RForest(X_train,X_test, y_train):
    rf = RandomForestClassifier(n_estimators=50, random_state=2)
    rf.fit(X_train, y_train)
    y_pred = rf.predict(X_test)
    return rf,y_pred
In [34]:
# Depression
model,y_pred=RForest(X_train_dep,X_test_dep, y_train_dep)
# filename="models/dep_model_rf.pkl"
# save_model(model,filename)
rf_acc_dep=accuracy_score(y_test_dep,y_pred)
rf_cls_dep=classification_report(y_test_dep, y_pred)
In [35]:
# Anxiety
model,y_pred=RForest(X_train_anx,X_test_anx, y_train_anx)
# filename="models/anx_model_rf.pkl"
# save_model(model,filename)
rf_acc_anx=accuracy_score(y_test_anx,y_pred) 
rf_cls_anx=classification_report(y_test_anx, y_pred)
In [36]:
# Stress
model,y_pred=RForest(X_train_str,X_test_str, y_train_str)
# filename="models/str_model_dt.pkl"
# save_model(model,filename)
rf_acc_str=accuracy_score(y_test_str,y_pred)
rf_cls_str=classification_report(y_test_str, y_pred)

SVM

In [58]:
from sklearn import svm
In [62]:
def SVM(X_train,X_test, y_train):
    sv = svm.SVC(probability=True, kernel='linear')
    sv.fit(X_train, y_train)
    y_pred = sv.predict(X_test)
    return sv,y_pred
In [63]:
# Depression
model,y_pred=SVM(X_train_dep,X_test_dep, y_train_dep)
# filename="models/dep_model_svm.pkl"
# save_model(model,filename)
svm_acc_dep=accuracy_score(y_test_dep,y_pred)
svm_cls_dep=classification_report(y_test_dep, y_pred)
In [39]:
# Anxiety
model,y_pred=SVM(X_train_anx,X_test_anx, y_train_anx)
# filename="models/anx_model_svm.pkl"
# save_model(model,filename)
svm_acc_anx=accuracy_score(y_test_anx,y_pred) 
svm_cls_anx=classification_report(y_test_anx, y_pred)
In [40]:
# Stress
model,y_pred=SVM(X_train_str,X_test_str, y_train_str)
# filename="models/str_model_svm.pkl"
# save_model(model,filename)
svm_acc_str=accuracy_score(y_test_str,y_pred)
svm_cls_str=classification_report(y_test_str, y_pred)

Bagging Method

In [41]:
def bagging(X_train,X_test, y_train):
    estimators = []
    model1= KNeighborsClassifier(n_neighbors=3) 
    estimators.append(('KNN', model1)) 
    model2 = DecisionTreeClassifier() 
    estimators.append(('cart',model2)) 
    model3 = SVC(probability=True, kernel='linear')
    estimators.append(('svm', model3))
    ensemble = VotingClassifier(estimators) #bagging ensembLe 
    eclf1 = ensemble.fit(X_train,y_train) 
    y_pred=eclf1.predict(X_test)
    return ensemble,y_pred
In [42]:
# Depression
model,y_pred=bagging(X_train_dep,X_test_dep, y_train_dep)
# filename="models/dep_model_ens.pkl"
# save_model(model,filename)
ens_acc_dep=accuracy_score(y_test_dep,y_pred)
ens_cls_dep=classification_report(y_test_dep, y_pred)
In [43]:
# Anxiety
model,y_pred=bagging(X_train_anx,X_test_anx, y_train_anx)
# filename="models/anx_model_ens.pkl"
# save_model(model,filename)
ens_acc_anx=accuracy_score(y_test_anx,y_pred) 
ens_cls_anx=classification_report(y_test_anx, y_pred)
In [44]:
# Stress
model,y_pred=bagging(X_train_str,X_test_str, y_train_str)
# filename="models/str_model_ens.pkl"
# save_model(model,filename)
ens_acc_str=accuracy_score(y_test_str,y_pred)
ens_cls_str=classification_report(y_test_str, y_pred)

Adaboost Classifying with base estimator as SVM

In [45]:
def AdaBoost(X_train,X_test, y_train):
    svc=SVC(probability=True, kernel='linear')
    abc = AdaBoostClassifier(n_estimators=10 ,base_estimator=svc)
    abc.fit(X_train, y_train)
    y_pred = abc.predict(X_test)
    return abc,y_pred
In [14]:
# Depression
model,y_pred=AdaBoost(X_train_dep,X_test_dep, y_train_dep)
# filename="models/dep_model_ada.pkl"
# save_model(model,filename)
ada_acc_dep=accuracy_score(y_test_dep,y_pred)
ada_cls_dep=classification_report(y_test_dep, y_pred)
# filename="models/dep_model_ada.joblib"
# dump_joblib(model,filename)
In [16]:
ada_acc_dep
Out[16]:
0.9995316159250586
In [17]:
# Anxiety
model,y_pred=AdaBoost(X_train_anx,X_test_anx, y_train_anx)
# filename="models/anx_model_ada.pkl"
# save_model(model,filename)
ada_acc_anx=accuracy_score(y_test_anx,y_pred) 
ada_cls_anx=classification_report(y_test_anx, y_pred)
# filename="models/anx_model_ada.joblib"
# dump_joblib(model,filename)
In [19]:
ada_acc_anx
Out[19]:
1.0
In [20]:
# Stress
model,y_pred=AdaBoost(X_train_str,X_test_str, y_train_str)
# filename="models/str_model_ada.pkl"
# save_model(model,filename)
ada_acc_str=accuracy_score(y_test_str,y_pred)
ada_cls_str=classification_report(y_test_str, y_pred)
# filename="models/str_model_ada.joblib"
# dump_joblib(model,filename)
In [21]:
ada_acc_str
Out[21]:
0.9805620608899297
In [49]:
model = load_model("../models/dep_model_ada.pkl")
y_pred = model.predict(X_test_dep)
ada_acc_dep=accuracy_score(y_test_dep,y_pred)
ada_cls_dep=classification_report(y_test_dep, y_pred)
In [50]:
model = load_model("../models/anx_model_ada.pkl")
y_pred = model.predict(X_test_anx)
ada_acc_anx=accuracy_score(y_test_anx,y_pred) 
ada_cls_anx=classification_report(y_test_anx, y_pred)
In [51]:
model = load_model("../models/str_model_ada.pkl")
y_pred = model.predict(X_test_str)
ada_acc_str=accuracy_score(y_test_str,y_pred)
ada_cls_str=classification_report(y_test_str, y_pred)

printing accuracy of each model

In [55]:
print("Depression accuracy for KNN: ",knn_acc_dep)
print("Anxiety accuracy for KNN: ",knn_acc_anx)
print("Stress accuracy for KNN: ",knn_acc_str)
print("\n\n")
print("Depression accuracy for Naive Bayes: ",gnb_acc_dep)
print("Anxiety accuracy for Naive Bayes: ",gnb_acc_anx)
print("Stress accuracy for Naive Bayes: ",gnb_acc_str)
print("\n\n")
print("Depression accuracy for Decision Tree using Gini: ",dt_acc_dep)
print("Anxiety accuracy for Decision Tree using Gini: ",dt_acc_anx)
print("Stress accuracy for Decision Tree using Gini: ",dt_acc_str)
print("\n\n")
print("Depression accuracy for Decision Tree using Entropy: ",dt_ent_acc_dep)
print("Anxiety accuracy for Decision Tree using Entropy: ",dt_ent_acc_anx)
print("Stress accuracy for Decision Tree using Entropy: ",dt_ent_acc_str)
print("\n\n")
print("Depression accuracy for Random Forest: ",rf_acc_dep)
print("Anxiety accuracy for Random Forest: ",rf_acc_anx)
print("Stress accuracy for Random Forest: ",rf_acc_str)
print("\n\n")
print("Depression accuracy for SVM: ",svm_acc_dep)
print("Anxiety accuracy for SVM: ",svm_acc_anx)
print("Stress accuracy for SVM: ",svm_acc_str)
print("\n\n")
print("Depression accuracy for Bagging Method: ",ens_acc_dep)
print("Anxiety accuracy for Bagging Method: ",ens_acc_anx)
print("Stress accuracy for Bagging Method: ",ens_acc_str)
print("\n\n")
print("Depression accuracy for Adaboosting: ",ada_acc_dep)
print("Anxiety accuracy for Adaboosting: ",ada_acc_anx)
print("Stress accuracy for Adaboosting: ",ada_acc_str)
Depression accuracy for KNN:  0.9446135831381733
Anxiety accuracy for KNN:  0.922599531615925
Stress accuracy for KNN:  0.9236533957845433



Depression accuracy for Naive Bayes:  0.9025761124121779
Anxiety accuracy for Naive Bayes:  0.813231850117096
Stress accuracy for Naive Bayes:  0.9019906323185012



Depression accuracy for Decision Tree using Gini:  0.8846604215456675
Anxiety accuracy for Decision Tree using Gini:  0.8464871194379391
Stress accuracy for Decision Tree using Gini:  0.8442622950819673



Depression accuracy for Decision Tree using Entropy:  0.884543325526932
Anxiety accuracy for Decision Tree using Entropy:  0.8682669789227166
Stress accuracy for Decision Tree using Entropy:  0.8449648711943794



Depression accuracy for Random Forest:  0.9673302107728338
Anxiety accuracy for Random Forest:  0.9612412177985948
Stress accuracy for Random Forest:  0.955503512880562



Depression accuracy for SVM:  1.0
Anxiety accuracy for SVM:  1.0
Stress accuracy for SVM:  1.0



Depression accuracy for Bagging Method:  0.9807962529274005
Anxiety accuracy for Bagging Method:  0.9723653395784543
Stress accuracy for Bagging Method:  0.9734192037470726



Depression accuracy for Adaboosting:  1.0
Anxiety accuracy for Adaboosting:  1.0
Stress accuracy for Adaboosting:  1.0

Printing classification report for each model

In [57]:
print("Depression Classification Report for KNN: \n",knn_cls_dep)
print("Anxiety Classification Report for KNN:\n ",knn_cls_anx)
print("Stress Classification Report for KNN:\n ",knn_cls_str)
print("\n\n")
print("Depression Classification Report for Naive Bayes: \n",gnb_cls_dep)
print("Anxiety Classification Report for Naive Bayes: \n",gnb_cls_anx)
print("Stress Classification Report for Naive Bayes: \n",gnb_cls_str)
print("\n\n")
print("Depression Classification Report for Decision Tree using Gini: \n",dt_cls_dep)
print("Anxiety Classification Report for Decision Tree using Gini: \n",dt_cls_anx)
print("Stress Classification Report for Decision Tree using Gini: \n",dt_cls_str)
print("\n\n")
print("Depression Classification Report for Decision Tree using Entropy: \n",dt_ent_cls_dep)
print("Anxiety Classification Report for Decision Tree using Entropy: \n",dt_ent_cls_anx)
print("Stress Classification Report for Decision Tree using Entropy: \n",dt_ent_cls_str)
print("\n\n")
print("Depression Classification Report for Random Forest: \n",rf_cls_dep)
print("Anxiety Classification Report for Random Forest:\n ",rf_cls_anx)
print("Stress Classification Report for Random Forest: \n",rf_cls_str)
print("\n\n")
print("Depression Classification Report for SVM:\n ",svm_cls_dep)
print("Anxiety Classification Report for SVM:\n ",svm_cls_anx)
print("Stress Classification Report for SVM: \n",knn_cls_str)
print("\n\n")
print("Depression Classification Report for Bagging Method: \n",ens_cls_dep)
print("Anxiety Classification Report for Bagging Method:\n ",ens_cls_anx)
print("Stress Classification Report for Bagging Method: \n",ens_cls_str)
print("\n\n")
print("Depression Classification Report for Adaboosting: \n",ada_cls_dep)
print("Anxiety Classification Report for Adaboosting: \n",ada_cls_anx)
print("Stress Classification Report for Adaboosting: \n",ada_cls_str)
Depression Classification Report for KNN: 
               precision    recall  f1-score   support

           0       0.96      1.00      0.98      1911
           1       0.89      0.90      0.90       868
           2       0.93      0.92      0.93      1794
           3       0.89      0.87      0.88      1206
           4       0.98      0.97      0.97      2761

    accuracy                           0.94      8540
   macro avg       0.93      0.93      0.93      8540
weighted avg       0.94      0.94      0.94      8540

Anxiety Classification Report for KNN:
                precision    recall  f1-score   support

           0       0.96      1.00      0.98      2084
           1       0.87      0.87      0.87       594
           2       0.87      0.93      0.90      1652
           3       0.78      0.72      0.75      1030
           4       0.98      0.94      0.96      3180

    accuracy                           0.92      8540
   macro avg       0.89      0.89      0.89      8540
weighted avg       0.92      0.92      0.92      8540

Stress Classification Report for KNN:
                precision    recall  f1-score   support

           0       0.95      0.99      0.97      2957
           1       0.83      0.83      0.83      1085
           2       0.87      0.85      0.86      1546
           3       0.93      0.90      0.92      1742
           4       0.98      0.96      0.97      1210

    accuracy                           0.92      8540
   macro avg       0.91      0.91      0.91      8540
weighted avg       0.92      0.92      0.92      8540




Depression Classification Report for Naive Bayes: 
               precision    recall  f1-score   support

           0       0.99      0.89      0.94      1911
           1       0.76      0.82      0.79       868
           2       0.87      0.95      0.91      1794
           3       0.76      0.92      0.83      1206
           4       1.00      0.90      0.95      2761

    accuracy                           0.90      8540
   macro avg       0.88      0.90      0.88      8540
weighted avg       0.91      0.90      0.91      8540

Anxiety Classification Report for Naive Bayes: 
               precision    recall  f1-score   support

           0       0.94      0.90      0.92      2084
           1       0.17      0.02      0.04       594
           2       0.61      0.95      0.74      1652
           3       0.62      0.57      0.60      1030
           4       0.98      0.91      0.95      3180

    accuracy                           0.81      8540
   macro avg       0.66      0.67      0.65      8540
weighted avg       0.80      0.81      0.79      8540

Stress Classification Report for Naive Bayes: 
               precision    recall  f1-score   support

           0       1.00      0.94      0.97      2957
           1       0.79      0.78      0.78      1085
           2       0.81      0.93      0.87      1546
           3       0.86      0.94      0.90      1742
           4       0.98      0.83      0.90      1210

    accuracy                           0.90      8540
   macro avg       0.89      0.88      0.88      8540
weighted avg       0.91      0.90      0.90      8540




Depression Classification Report for Decision Tree using Gini: 
               precision    recall  f1-score   support

           0       0.96      0.96      0.96      1911
           1       0.78      0.75      0.77       868
           2       0.81      0.88      0.85      1794
           3       0.78      0.73      0.75      1206
           4       0.96      0.95      0.95      2761

    accuracy                           0.88      8540
   macro avg       0.86      0.85      0.85      8540
weighted avg       0.88      0.88      0.88      8540

Anxiety Classification Report for Decision Tree using Gini: 
               precision    recall  f1-score   support

           0       0.93      0.97      0.95      2084
           1       0.63      0.50      0.56       594
           2       0.77      0.78      0.78      1652
           3       0.60      0.67      0.63      1030
           4       0.96      0.92      0.94      3180

    accuracy                           0.85      8540
   macro avg       0.78      0.77      0.77      8540
weighted avg       0.85      0.85      0.85      8540

Stress Classification Report for Decision Tree using Gini: 
               precision    recall  f1-score   support

           0       0.95      0.93      0.94      2957
           1       0.64      0.73      0.68      1085
           2       0.75      0.73      0.74      1546
           3       0.84      0.82      0.83      1742
           4       0.91      0.92      0.92      1210

    accuracy                           0.84      8540
   macro avg       0.82      0.83      0.82      8540
weighted avg       0.85      0.84      0.85      8540




Depression Classification Report for Decision Tree using Entropy: 
               precision    recall  f1-score   support

           0       0.95      0.96      0.96      1911
           1       0.75      0.78      0.77       868
           2       0.85      0.83      0.84      1794
           3       0.76      0.78      0.77      1206
           4       0.96      0.95      0.96      2761

    accuracy                           0.88      8540
   macro avg       0.85      0.86      0.86      8540
weighted avg       0.89      0.88      0.88      8540

Anxiety Classification Report for Decision Tree using Entropy: 
               precision    recall  f1-score   support

           0       0.96      0.98      0.97      2084
           1       0.72      0.72      0.72       594
           2       0.79      0.81      0.80      1652
           3       0.62      0.67      0.64      1030
           4       0.96      0.92      0.94      3180

    accuracy                           0.87      8540
   macro avg       0.81      0.82      0.82      8540
weighted avg       0.87      0.87      0.87      8540

Stress Classification Report for Decision Tree using Entropy: 
               precision    recall  f1-score   support

           0       0.96      0.92      0.94      2957
           1       0.66      0.75      0.71      1085
           2       0.74      0.77      0.75      1546
           3       0.84      0.79      0.81      1742
           4       0.90      0.92      0.91      1210

    accuracy                           0.84      8540
   macro avg       0.82      0.83      0.82      8540
weighted avg       0.85      0.84      0.85      8540




Depression Classification Report for Random Forest: 
               precision    recall  f1-score   support

           0       0.99      1.00      0.99      1911
           1       0.94      0.95      0.95       868
           2       0.95      0.96      0.96      1794
           3       0.93      0.92      0.92      1206
           4       0.98      0.98      0.98      2761

    accuracy                           0.97      8540
   macro avg       0.96      0.96      0.96      8540
weighted avg       0.97      0.97      0.97      8540

Anxiety Classification Report for Random Forest:
                precision    recall  f1-score   support

           0       1.00      1.00      1.00      2084
           1       0.96      0.98      0.97       594
           2       0.93      0.97      0.95      1652
           3       0.86      0.85      0.86      1030
           4       0.99      0.97      0.98      3180

    accuracy                           0.96      8540
   macro avg       0.95      0.95      0.95      8540
weighted avg       0.96      0.96      0.96      8540

Stress Classification Report for Random Forest: 
               precision    recall  f1-score   support

           0       0.98      0.99      0.98      2957
           1       0.91      0.90      0.90      1085
           2       0.92      0.91      0.92      1546
           3       0.95      0.95      0.95      1742
           4       0.99      0.99      0.99      1210

    accuracy                           0.96      8540
   macro avg       0.95      0.95      0.95      8540
weighted avg       0.96      0.96      0.96      8540




Depression Classification Report for SVM:
                precision    recall  f1-score   support

           0       1.00      1.00      1.00      1911
           1       1.00      1.00      1.00       868
           2       1.00      1.00      1.00      1794
           3       1.00      1.00      1.00      1206
           4       1.00      1.00      1.00      2761

    accuracy                           1.00      8540
   macro avg       1.00      1.00      1.00      8540
weighted avg       1.00      1.00      1.00      8540

Anxiety Classification Report for SVM:
                precision    recall  f1-score   support

           0       1.00      1.00      1.00      2084
           1       1.00      1.00      1.00       594
           2       1.00      1.00      1.00      1652
           3       1.00      1.00      1.00      1030
           4       1.00      1.00      1.00      3180

    accuracy                           1.00      8540
   macro avg       1.00      1.00      1.00      8540
weighted avg       1.00      1.00      1.00      8540

Stress Classification Report for SVM: 
               precision    recall  f1-score   support

           0       0.95      0.99      0.97      2957
           1       0.83      0.83      0.83      1085
           2       0.87      0.85      0.86      1546
           3       0.93      0.90      0.92      1742
           4       0.98      0.96      0.97      1210

    accuracy                           0.92      8540
   macro avg       0.91      0.91      0.91      8540
weighted avg       0.92      0.92      0.92      8540




Depression Classification Report for Bagging Method: 
               precision    recall  f1-score   support

           0       0.99      1.00      0.99      1911
           1       0.96      0.98      0.97       868
           2       0.98      0.97      0.97      1794
           3       0.96      0.95      0.95      1206
           4       0.99      0.99      0.99      2761

    accuracy                           0.98      8540
   macro avg       0.98      0.98      0.98      8540
weighted avg       0.98      0.98      0.98      8540

Anxiety Classification Report for Bagging Method:
                precision    recall  f1-score   support

           0       0.99      1.00      1.00      2084
           1       0.97      0.98      0.98       594
           2       0.95      0.99      0.97      1652
           3       0.91      0.89      0.90      1030
           4       0.99      0.97      0.98      3180

    accuracy                           0.97      8540
   macro avg       0.96      0.97      0.96      8540
weighted avg       0.97      0.97      0.97      8540

Stress Classification Report for Bagging Method: 
               precision    recall  f1-score   support

           0       0.98      1.00      0.99      2957
           1       0.94      0.94      0.94      1085
           2       0.96      0.94      0.95      1546
           3       0.98      0.97      0.97      1742
           4       0.99      1.00      0.99      1210

    accuracy                           0.97      8540
   macro avg       0.97      0.97      0.97      8540
weighted avg       0.97      0.97      0.97      8540




Depression Classification Report for Adaboosting: 
               precision    recall  f1-score   support

           0       1.00      1.00      1.00      1911
           1       1.00      1.00      1.00       868
           2       1.00      1.00      1.00      1794
           3       1.00      1.00      1.00      1206
           4       1.00      1.00      1.00      2761

    accuracy                           1.00      8540
   macro avg       1.00      1.00      1.00      8540
weighted avg       1.00      1.00      1.00      8540

Anxiety Classification Report for Adaboosting: 
               precision    recall  f1-score   support

           0       1.00      1.00      1.00      2084
           1       1.00      1.00      1.00       594
           2       1.00      1.00      1.00      1652
           3       1.00      1.00      1.00      1030
           4       1.00      1.00      1.00      3180

    accuracy                           1.00      8540
   macro avg       1.00      1.00      1.00      8540
weighted avg       1.00      1.00      1.00      8540

Stress Classification Report for Adaboosting: 
               precision    recall  f1-score   support

           0       1.00      1.00      1.00      2957
           1       1.00      1.00      1.00      1085
           2       1.00      1.00      1.00      1546
           3       1.00      1.00      1.00      1742
           4       1.00      1.00      1.00      1210

    accuracy                           1.00      8540
   macro avg       1.00      1.00      1.00      8540
weighted avg       1.00      1.00      1.00      8540

saving the accuracy in a dataframe

In [58]:
x=['KNN','GaussianNB','Decision Tree(Gini)','Decision Tree(Entropy)','Random Forest','SVM','Bagging','Adaboost']
ydep=[knn_acc_dep,gnb_acc_dep,dt_acc_dep,dt_ent_acc_dep,rf_acc_dep,svm_acc_dep,ens_acc_dep,ada_acc_dep]
yanx=[knn_acc_anx,gnb_acc_anx,dt_acc_anx,dt_ent_acc_anx,rf_acc_anx,svm_acc_anx,ens_acc_anx,ada_acc_anx]
ystr=[knn_acc_str,gnb_acc_str,dt_acc_str,dt_ent_acc_str,rf_acc_str,svm_acc_str,ens_acc_str,ada_acc_str]
In [59]:
data = {'Algorithms':x,
        'DepressionAccuracy':ydep,
        'AnxietyAccuracy':yanx,
       'StressAccuracy':ystr}
In [60]:
df_acc=pd.DataFrame(data)
df_acc.head()
Out[60]:
Algorithms DepressionAccuracy AnxietyAccuracy StressAccuracy
0 KNN 0.944614 0.922600 0.923653
1 GaussianNB 0.902576 0.813232 0.901991
2 Decision Tree(Gini) 0.884660 0.846487 0.844262
3 Decision Tree(Entropy) 0.884543 0.868267 0.844965
4 Random Forest 0.967330 0.961241 0.955504
In [61]:
df_acc.to_csv('Accuracy.csv',index=False)

Comparison of accuracy for each model using a bar graph

In [4]:
df_acc=reading_csv('../Datasets/Accuracy.csv')
In [8]:
colors = ['#274472', '#5885AF', '#C3E0E5','#007e79']
fig = go.Figure(data=[
    go.Bar(name='Depression', x=df_acc['Algorithms'], y=df_acc['DepressionAccuracy'],marker_color=colors[0]),
    go.Bar(name='Stress', x=df_acc['Algorithms'], y=df_acc['AnxietyAccuracy'],marker_color=colors[1]),
    go.Bar(name='Anxiety', x=df_acc['Algorithms'], y=df_acc['StressAccuracy'],marker_color=colors[2]),
])
# Change the bar mode
fig.update_layout(barmode='group')
fig.write_image("images/acc.png")
fig.show()
In [ ]: